const { msg } = require("../game/alert");
const { times } = require("../shop/gameData");

require("../AnyThinkAds/ATRewardedVideoJSSDK"); 

const adMap = {
    map:["b60cd3c040186f", "f60cd3c6670ba7"],
    speed: ["b60cd3c15b2923", "f60cd3c4679d92"],
    support: ["b60e5b56ba52fa", "f60e5c6a918bef"],
    shopFamous: ["b60e5b0ff94c8c", "f60e5c68143536"],//店铺宣传
    money: ["b6098dd17a0c04", "f609e8e93421aa"],
    openShop: ["b6098dd174f06d", "f60e5c76fe1d86"]//开店
}
class VideoAds {
  
    placementID = "b60cd3c15b2923";
    callback = null;
    constructor() {
        // rewardedVideoListener.onReward = callback;
    }
    init() {
        if(cc.sys.isNative){
            ATRewardedVideoJSSDK.setAdListener(this);
            this.loadAds();
        }
    }
     loadAds() {
        var setting = {};
        setting[ATRewardedVideoJSSDK.userIdKey] = "test_user_id";
        setting[ATRewardedVideoJSSDK.userDataKey] = "test_user_data";
        ATRewardedVideoJSSDK.loadRewardedVideo(this.placementID, setting);
    }
    showAd(cb) {
        // ATRewardedVideoJSSDK.showAd(this.placementID());
        // if(times.count <= 0){
        //     msg("今天的广告用完了噢,一天4次");
        //     return ;
        // }
        if(!this.checkReady) {
            this.loadAds();
        }
<<<<<<< HEAD
        times.count --;
        ATRewardedVideoJSSDK.showAdInScenario(this.placementID, "f60cd3c4679d92");
=======
        ATRewardedVideoJSSDK.showAdInScenario(this.placementID, "f60cd3c4679d92");
        // times.count --;
>>>>>>> 9551c9ec11d53d5633de75fa89402c53378c5072
        this.callback = cb;
    }
    // cb(id, msg){
    //     cc.log('观看完成');
    //     this.callback(msg);
    // }
    checkReady() {
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::checkReady()   " + (ATRewardedVideoJSSDK.hasAdReady(this.placementID) ? true : false));

        var adStatusInfo = ATRewardedVideoJSSDK.checkAdStatus(this.placementID);
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::checkAdStatus()   " + adStatusInfo);
        return ATRewardedVideoJSSDK.hasAdReady(this.placementID) ? true : false;
    }
    onRewardedVideoAdLoaded(id) {
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdLoaded(" + id + ")");
    }
    //广告加载失败
    onRewardedVideoAdFailed(placementId, errorInfo) {
      ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdFailed(" + placementId + ", " + errorInfo + ")");
    }
    //广告开始播放
    onRewardedVideoAdPlayStart(placementId, callbackInfo) {
       ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayStart(" + placementId + ", " + callbackInfo + ")");
    }
    //广告播放结束
    onRewardedVideoAdPlayEnd(placementId, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayEnd(" + placementId + ", " + callbackInfo + ")");
    }
    //广告播放失败
    onRewardedVideoAdPlayFailed(placementId, errorInfo, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayFailed(" + placementId + ", " + errorInfo + ", " + callbackInfo + ")");
    }
    //广告被关闭
    onRewardedVideoAdClosed(placementId, callbackInfo) {
        this.loadAds();
    }
    //广告被点击
    onRewardedVideoAdPlayClicked(placementId, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayClicked(" + placementId + ", " + callbackInfo + ")");
    }
    //激励成功，开发者可在此回调中下发奖励，一般先于onRewardedVideoAdClosed回调，服务器激励则不一定
    onReward(placementId, callbackInfo) {
        // this.callback.call(this, callbackInfo);
        this.callback(callbackInfo);
        cc.log('播放完成')
        // this.cb(callbackInfo);
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onReward(" + placementId + ", " + callbackInfo + ")");
    }
    // setCallback(cb) {
    //     rewardedVideoListener.onReward = cb;
    // }
}
let Video = new VideoAds();
var rewardedVideoListener = {
    //广告加载成功
    onRewardedVideoAdLoaded:function(placementId) {
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdLoaded(" + placementId + ")");
    },
    //广告加载失败
    onRewardedVideoAdFailed : function(placementId, errorInfo) {
      ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdFailed(" + placementId + ", " + errorInfo + ")");
    },
    //广告开始播放
    onRewardedVideoAdPlayStart : function(placementId, callbackInfo) {
       ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayStart(" + placementId + ", " + callbackInfo + ")");
    },
    //广告播放结束
    onRewardedVideoAdPlayEnd : function(placementId, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayEnd(" + placementId + ", " + callbackInfo + ")");
    },
    //广告播放失败
    onRewardedVideoAdPlayFailed : function(placementId, errorInfo, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayFailed(" + placementId + ", " + errorInfo + ", " + callbackInfo + ")");
    },
    //广告被关闭
    onRewardedVideoAdClosed : Video.onRewardedVideoAdClosed,
    //广告被点击
    onRewardedVideoAdPlayClicked : Video.onRewardedVideoAdPlayClicked,
    //激励成功，开发者可在此回调中下发奖励，一般先于onRewardedVideoAdClosed回调，服务器激励则不一定
    onReward : Video.onReward,
}
if(cc.sys.isNative){
    // ATRewardedVideoJSSDK.setAdListener(rewardedVideoListener);
}

module.exports.videoAds = Video;
